﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace BaseTypesLib.Model
{
    [DataContract(IsReference = true)]
    public class NumericChoiceField:ChoiceField
    {
        private int start, end, increment;

        [DataMember]
        public int Start
        {
            get { return start; }
            set
            {
                if (start != value)
                {
                    start = value;
                    OnPropertyChanged("Start");
                }
            }
        }

        [DataMember]
        public int End
        {
            get { return end; }
            set
            {
                if (end != value)
                {
                    end = value;
                    OnPropertyChanged("End");
                }
            }
        }

        [DataMember]
        public int Increment
        {
            get { return increment; }
            set
            {
                if (increment != value)
                {
                    increment = value;
                    OnPropertyChanged("Increment");
                }
            }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();

            if (Start >= 0)
                el.SetAttributeValue("Start", Start);
            if (End >= 0)
                el.SetAttributeValue("End", End);
            if (Increment >= 1)
                el.SetAttributeValue("Increment", Increment);
            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var lbl = elem.Attributes("Label").FirstOrDefault();
            if (lbl != null)
                Label = lbl.Value;

            var start = elem.Attributes("Start").FirstOrDefault();
            if (start != null) Start = (int)start;

            var end = elem.Attributes("End").FirstOrDefault();
            if (end != null) End = (int)end;
            var incr = elem.Attributes("Increment").FirstOrDefault();
            if (incr != null) Increment = (int)incr;
        }
    }
}
